package com.elinkway.infinitemovies.http.parser;

import android.text.TextUtils;
import android.util.Base64;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.bean.CloudDiskBean;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CloudDiskParser extends LVideoMobileParser<CloudDiskBean> {

    private String currClarity = "";

    public CloudDiskParser(){
        super();
    }

    public CloudDiskParser(String currClarity){
        super();
        this.currClarity = currClarity;
    }

    @Override
    public JSONObject getData(String data) throws JSONException {
        byte[] decodeData = Base64.decode(data, Base64.DEFAULT);
        String result = Utils.AES256_decode(decodeData, Utils.AES_KEY);
        if (!TextUtils.isEmpty(result)) {
            return new JSONObject(result);
        }
        return null;
    }

    @Override
    public CloudDiskBean parse(JSONObject data) throws Exception {
        LogUtils.e("wym", "cloud parser data is " + data);
        // cloud 数据太长 存入本地
        Utils.writeLogFile(MoviesApplication.getInstance().getApplicationContext(), "CloudParserData.txt", data.toString());
        JSONObject dataObj = data.optJSONObject("data");
        JSONObject videoListObj = dataObj.optJSONObject("video_list");
        CloudDiskBean diskBean = new CloudDiskBean();
        diskBean.setAllCloudData(data.toString());
        diskBean.setUser_id(videoListObj.optString("user_id"));
        diskBean.setVideo_id(videoListObj.optString("video_id"));
        diskBean.setMedia_id(videoListObj.optString("media_id"));
        diskBean.setDefault_play(videoListObj.optString("default_play"));
        diskBean.setVideo_duration(videoListObj.optString("video_duration"));
        diskBean.setVideo_name(videoListObj.optString("video_name"));
        diskBean.setType(videoListObj.optString("type"));

        Iterator<String> listKey = videoListObj.keys();
        while (listKey.hasNext()) {
            String key = listKey.next();
            Pattern p = Pattern.compile("video_\\d");
            Matcher m = p.matcher(key);
            if (m.find()) {
                CloudDiskBean.VideoListObj videoList
                        = parserVideoListObj(videoListObj.optJSONObject(key));
                if (videoList != null) {
                    diskBean.putSupportVideoListObj(key, videoList);
                }
            }
        }

        if(TextUtils.isEmpty(currClarity)){
            currClarity = data.optString("pls");
        }
        JSONObject videoObj = null;
        if(!TextUtils.isEmpty(currClarity)){
            videoObj = videoListObj.optJSONObject(currClarity);
            if(videoObj == null){
                videoObj = videoListObj.optJSONObject(diskBean.getDefault_play());
                diskBean.setCurrClarity(diskBean.getDefault_play());
            }else{
                diskBean.setCurrClarity(currClarity);
            }
        } else {
            videoObj = videoListObj.optJSONObject(diskBean.getDefault_play());
            diskBean.setCurrClarity(diskBean.getDefault_play());
        }
        LogUtils.e("wym", "video " + videoObj.toString());
        ArrayList<String> playUrls = new ArrayList<String>(4);

        String mainUrl = Utils.getBase64Decode(videoObj.optString("main_url"));
//        LogUtils.e("wym", "!!!!!!!!!!!!!!!!!!!!!parser before cloudMainUrl is " + videoObj.optString("main_url"));
        if (!TextUtils.isEmpty(mainUrl)) {
            playUrls.add(mainUrl);
            LogUtils.e("wym", "!!!!!!!!!!!!!!!!!!!!!cloudMainUrl is " + mainUrl);
//			playUrls.add("http://g3.lvideo.cn/vod/vasdsdsa2/NjgvMzYvasdsadasdasdsaODEvbGVEzOGFmMjBmLTE0MDI2NTEzMDYwNzkubXA0");
        }

        String backup_url1 = Utils.getBase64Decode(videoObj
                .optString("backup_url_1"));
        if (!TextUtils.isEmpty(backup_url1)) {
            playUrls.add(backup_url1);
//			playUrls.add("http://g3.lvideo.cn/vod/vasdsdsa2/NjgvMzYvasdsadasdasdsaODEvbGVEzOGFmMjBmLTE0MDI2NTEzMDYwNzkubXA0");
        }

        String backup_url2 = Utils.getBase64Decode(videoObj
                .optString("backup_url_2"));
        if (!TextUtils.isEmpty(backup_url2)) {
            playUrls.add(backup_url2);
//			playUrls.add("http://g3.lvideo.cn/vod/vasdsdsa2/NjgvMzYvasdsadasdasdsaODEvbGVEzOGFmMjBmLTE0MDI2NTEzMDYwNzkubXA0");
        }

        String backup_url3 = Utils.getBase64Decode(videoObj
                .optString("backup_url_3"));
        if (!TextUtils.isEmpty(backup_url3)) {
            playUrls.add(backup_url3);
        }

        diskBean.setmPlayUrls(playUrls);
        diskBean.setVwidth(videoObj.optString("vwidth"));
        diskBean.setVheight(videoObj.optString("vheight"));
        diskBean.setGbr(videoObj.optString("gbr"));
        diskBean.setStorePath(videoObj.optString("storePath"));
        diskBean.setVtype(videoObj.optString("vtype"));
        diskBean.setDefinition(videoObj.optString("definition"));
        return diskBean;
    }

    private CloudDiskBean.VideoListObj parserVideoListObj(JSONObject videoObj) {
        CloudDiskBean.VideoListObj videoListObj = new CloudDiskBean.VideoListObj();
        videoListObj.setBackupUrl1(Utils.getBase64Decode(videoObj
                .optString("backup_url_1")));
        videoListObj.setBackupUrl1(Utils.getBase64Decode(videoObj
                .optString("backup_url_2")));
        videoListObj.setBackupUrl1(Utils.getBase64Decode(videoObj
                .optString("backup_url_3")));
        videoListObj.setMainUrl(Utils.getBase64Decode(videoObj
                .optString("main_url")));
        videoListObj.setDefinition(videoObj.optString("definition"));
        videoListObj.setGbr(videoObj.optString("gbr"));
        videoListObj.setStorePath(videoObj.optString("storePath"));
        videoListObj.setVheight(videoObj.optString("vheight"));
        videoListObj.setVtype(videoObj.optString("vtype"));
        videoListObj.setVwidth(videoObj.optString("vwidth"));
        videoListObj.md5 = videoObj.optString("md5");
        videoListObj.gsize = videoObj.optString("gsize");
        return videoListObj;
    }
}
